package ssm.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import ssm.domain.Product;
import ssm.service.IProductService;

import javax.annotation.security.RolesAllowed;
import java.util.List;

@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private IProductService productService;

    //产品添加
    @RequestMapping("/save.do")
    public  String  save(Product product) throws Exception {
        productService.save(product);

        /*此处需要重新查询相关信息。即可以直接跳转到上次的查询所有页面*/
        return "redirect:findAll.do";
    }

   /* @RequestMapping("/findAll.do")
    public ModelAndView findAll() throws Exception {
        //1.由于此处使用的是ModelAndView。因此需要先创建该对象
        ModelAndView mv = new ModelAndView();

        List<Product> ps = productService.findAll();
        mv.addObject("productList",ps);
        mv.setViewName("product-list1");
        System.out.println("查询开始。。。");
        return mv;
    }*/

   /*进行分页处理*/
    @RequestMapping("/findAll.do")

    public ModelAndView findAll(@RequestParam(name = "page",required = true,defaultValue = "1") Integer page,
                                @RequestParam(name = "size",required = true,defaultValue = "4")Integer size
    ) throws Exception {
        //1.由于此处使用的是ModelAndView。因此需要先创建该对象
        ModelAndView mv = new ModelAndView();

        List<Product> ps = productService.findAll(page,size);

        //2.此处使用PageInfo处理相关信息
        PageInfo pageInfo = new PageInfo(ps);
        mv.addObject("pageInfo",pageInfo);
        mv.setViewName("product-page-list1");
        return mv;
    }

}
